﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Easyleave.Logic.Domain;

namespace Easyleave.WinformsTestClient
{
    public partial class MainForm : Form
    {
        LeaveWorkflowServiceClient wofkflowClient;
        MembershipServiceClient membershipClient;
        public UserDto User { get; set; }

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            wofkflowClient = new LeaveWorkflowServiceClient("WSHttpBinding_ILeaveWorkflowService");
            membershipClient = new MembershipServiceClient("WSHttpBinding_IMembershipService");
            this.Text = User.Fullname;
            LoadMyLeaves();
            LoadMyRecommendations();
            LoadMyApprovals();
        }

        public void LoadMyRecommendations()
        {
            IList<LeaveDto> list = wofkflowClient.GetLeavesToBeRecommendedByMe(User.Id);
            this.dataGridView2.DataSource = list;
        }

        public void LoadMyApprovals()
        {
            IList<LeaveDto> list = wofkflowClient.GetLeavesToBeApprovedByMe(User.Id);
            this.dataGridView3.DataSource = list;
        }

        public void LoadMyLeaves()
        {
            IList<LeaveDto> list = wofkflowClient.GetLeavesSubmittedByMe(User.Id);
            this.dataGridView1.DataSource = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewLeaveForm leaveForm = new NewLeaveForm();
            leaveForm.Types = membershipClient.GetLeaveTypes(User.Id);
            leaveForm.User = User;
            leaveForm.Parent = this;
            leaveForm.Show();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count == 1)
            {
                Guid leaveId = (Guid)dataGridView2.SelectedRows[0].Cells["WorkflowInstanceId"].Value;
                wofkflowClient.RecommendLeaveRequest(leaveId, User.Id, textBox1.Text);
            }
            else
            {
                MessageBox.Show("Please select a single row for recommendation");
            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            LoadMyLeaves();
            LoadMyRecommendations();
            LoadMyApprovals();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView3.SelectedRows.Count == 1)
            {
                Guid leaveId = (Guid)dataGridView3.SelectedRows[0].Cells["WorkflowInstanceId"].Value;
                wofkflowClient.ApproveLeaveRequest(leaveId, User.Id, textBox1.Text);
            }
            else
            {
                MessageBox.Show("Please select a single row for approval");
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView3.SelectedRows.Count == 1)
            {
                Guid leaveId = (Guid)dataGridView3.SelectedRows[0].Cells["WorkflowInstanceId"].Value;
                wofkflowClient.RejectLeaveRequest(leaveId, User.Id, textBox1.Text);
            }
            else
            {
                MessageBox.Show("Please select a single row for rejection");
            }
        }
    }
}
